// gcc data_test.c ../cstr.c ../data_s.c -std=c99
#include <wrdc.h>
#include "../cstr.h"
#include "../data_s.h"

/*-----------------------------------------------------------------*\
    定义数据结构
\*-----------------------------------------------------------------*/

typedef struct stack_s{  // 结构体容器主要用于存放指针数组
    cstr_t **start, **end;
    cstr_t **top;  // 指向待写入数据空间
} stack_t;

typedef struct queue_s{
    cstr_t **start, **end;
    cstr_t **head, **tail;
} queue_t;

void debug_stack(stack_t s){
    // 自栈顶遍历
    cstr_t **ppoint = s.start;
    while(ppoint != s.top){
        debug_cstr(**(ppoint++));
    }
}

void debug_queue(queue_t s){
    // 自栈顶遍历
    cstr_t **ppoint = s.head;
    while(ppoint != s.tail){
        debug_cstr(**(ppoint++));
    }
}

/*-----------------------------------------------------------------*\
    测试函数
\*-----------------------------------------------------------------*/

void test_stack(){
    stack_t tmp;
    cstr_t t0 = cstr("HELLO,");
    cstr_t t1 = cstr("WORLD.");
    cstr_t t2 = cstr("WELL-DONE\n");

    STACK_ALLOC(tmp, 20);
    STACK_PUSH(tmp, &t0);
    STACK_PUSH(tmp, &t1);
    STACK_POP(tmp);
    STACK_PUSH(tmp, &t2);
    _print("The stack size is : %d/%d", STACK_LENGTH(tmp), STACK_SIZE(tmp));
    debug_stack(tmp);

    debug_cstr(*STACK_TOP(tmp));

    STACK_FREE(tmp);
    Fn_apply(char, free, t0.start, t1.start, t2.start);
}

void test_queue(){
    queue_t tmp;
    cstr_t t0 = cstr("HELLO,");
    cstr_t t1 = cstr("WORLD.");
    cstr_t t2 = cstr("WELL-DONE\n");

    QUEUE_ALLOC(tmp, 20);
    ENQUEUE(tmp, &t0);
    ENQUEUE(tmp, &t1);
    DEQUEUE(tmp);
    QUEUE_INSERT(tmp, 0, &t2);
    _print("The queue size is : %d/%d", QUEUE_LENGTH(tmp), QUEUE_SIZE(tmp));
    debug_queue(tmp);

    debug_cstr(*QUEUE_BACK(tmp));
    debug_cstr(*QUEUE_FRONT(tmp));

    QUEUE_FREE(tmp);
    Fn_apply(char, free, t0.start, t1.start, t2.start);
}

int main(){
    test_stack();
    test_queue();
}
